package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Address;
import com.khshop.pojo.Cart;
import com.khshop.pojo.Order;
import com.khshop.service.AddressService;
import com.khshop.service.CartService;
import com.khshop.service.OrderService;
import com.khshop.service.ProductService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-16
 */
@RestController
@RequestMapping("/")
public class CartController {


    @Autowired
    CartService cartService;
    @Autowired
    AddressService addressService;
    @Autowired
    OrderService orderService;
    @Autowired
    ProductService productService;

    //新增购物车
    @ApiOperation("新增购物车")
    @PostMapping("/addCart")
    @ResponseBody
    public AjaxResult addCart(@RequestBody Cart cart) {
        if (cartService.addCart(cart).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //修改购物车
    @ApiOperation("修改购物车")
    @PostMapping("/updateCart")
    @ResponseBody
    public AjaxResult updateCart(@RequestBody Cart cart) {
        if (cartService.updateCart(cart).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //删除购物车
    @ApiOperation("删除购物车")
    @PostMapping("/delCart")
    @ResponseBody
    public AjaxResult delCart(Integer id) {
        if (cartService.delCart(id).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //查询购物车
    @ApiOperation("查询购物车")
    @PostMapping("/searchCart")
    @ResponseBody
    public AjaxResult searchCart(@RequestBody Map<String, String> map,
                                 @RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", defaultValue = "15") Integer limit) {

        ServiceResult<IPage<Cart>> serviceResult = cartService.searchCart(map, page, limit);
        if (serviceResult.isFlag()) {
            return AjaxResult.success((int) serviceResult.getData().getTotal(), serviceResult.getData());
        } else {
            return AjaxResult.error();
        }

    }


    //去结算
    @ApiOperation("下单与微信支付接口对接")
    @PostMapping("/toOrders")
    @ResponseBody
    public AjaxResult toOrders(Integer[] id, Long addressId) {
        ServiceResult<List<Cart>> cartLists = cartService.toOrders(id);
        if (!cartLists.isFlag()) {
            return AjaxResult.error("购物车为空，请稍后重试！");
        }
        ServiceResult<Address> serviceResult = addressService.getAddress(addressId);
        Address address = serviceResult.getData();
        String addr = address.getAddr();
        try {
            for (Cart cart : cartLists.getData()) {
                Order order = new Order();
                Long orderId=Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmssfff").format(new Date())+cart.getUserid()+(int)(Math.random()*(10)));
                order.setId(orderId);
                order.setAddr(addr);
                order.setUserid(cart.getUserid());
                order.setProdid(cart.getProdid());
                order.setNum(cart.getNum());
                productService.addProductStock(cart.getProdid(),-cart.getNum());
                orderService.addOrder(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("下单失败，请稍后重试！");
        }
        cartService.clearCart(Math.toIntExact(address.getUserid()));
        return AjaxResult.success("下单成功");
    }

    //清空购物车
    @ApiOperation("清空购物车")
    @PostMapping("/clearCart")
    @ResponseBody
    public AjaxResult clearCart(Integer userid) {
        if (cartService.clearCart(userid).isFlag()) {
            return AjaxResult.success("清空成功");
        } else {
            return AjaxResult.error("清空失败，请稍后重试！");
        }
    }
}

